import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import { message } from "ant-design-vue/es";

// import qs from 'qs';
// 后台
const showStatus = (status: number) => {
    let message = ''
    switch (status) {
        case 400:
            message = '请求错误(400)'
            break
        case 401:
            message = '未授权，请重新登录(401)'
            break
        case 403:
            message = '拒绝访问(403)'
            break
        case 404:
            message = '请求出错(404)'
            break
        case 408:
            message = '请求超时(408)'
            break
        case 500:
            message = '服务器错误(500)'
            break
        case 501:
            message = '服务未实现(501)'
            break
        case 502:
            message = '网络错误(502)'
            break
        case 503:
            message = '服务不可用(503)'
            break
        case 504:
            message = '网络超时(504)'
            break
        case 505:
            message = 'HTTP版本不受支持(505)'
            break
        default:
            message = `连接出错(${status})!`
    }
    return `${message}，请检查网络或联系hjy！`
}
let baseURL: string;
if (process.env.NODE_ENV == "development") {
    baseURL = "http://add.com";
    // baseURL = "http://45.253.64.106:81/";
} else {
    baseURL = "http://45.253.64.106:84/";
}
interface Iresponse {
    code: number;
    msg: string;
}
const api = axios.create({
    baseURL,
    timeout: 50000,
    headers: {
        get: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        post: {
            'Content-Type': 'application/json;charset=utf-8'
        }
    },
    //没有遇到 遇到再开启
    // validateStatus() {
    //     // 使用async-await，处理reject情况较为繁琐，所以全部返回resolve，在业务代码中处理异常
    //     return true
    // },
    transformResponse: [(data) => {
        // 判断种类过滤

        try {
            if (typeof data === 'string') {
                data = JSON.parse(data)
            }
        } catch (error) {
            console.log("错误"+data);
            message.error("出错了api设置");
        }
        console.log(data);
        showStatus(data.status);
        if (data.message && data.status == "success") message.success(data.message);
        else if (data.message && data.status == "error") message.error(data.message);
        return data;

    }],

})
axios.interceptors.request.use((config: AxiosRequestConfig) => {
    // 在发送请求之前做些什么
    //查看token
    // let token: string = localStorage.getItem("token");
    console.log(config);
    console.log(config);
    console.log(config);
    console.log(config);
    message.error("AxiosRequestConfig");
    return config
}), ((e: any) => {
    console.log(e);
    console.log(e);
    console.log(e);
    console.log(e);
});
axios.interceptors.response.use((res: AxiosResponse): any => {
    //输出数据
    //具体怎么设置的要看一下
    // res['status'] = 99999;
    message.error("AxiosResponse");
    console.log(res.status);
    console.log(res);
    console.log(res);
    console.log(res);
    console.log(res);
    // return "123";
    return res;
    // const response: Iresponse = res as Iresponse;
    // if (response.code !== 0) {

    // }
    // return response;
}, (error) => {
    if (axios.isCancel(error)) {
        console.log('repeated request: ' + error.message)
    } else {
        // handle error code
        // 错误抛到业务代码
        error.data = {}
        error.data.msg = '请求超时或服务器异常，请检查网络或联系管理员！'
        // ElMe ssage.error(error.data.msg)
    }
    return Promise.reject(error)
})
export default api;

